/*
 */

package com.railway.msg.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import cn.org.rapid_framework.page.Page;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.railway.common.base.BaseStruts2Action;
import com.railway.common.constants.SysConstant;
import com.railway.common.util.Json;
import com.railway.msg.constants.IConstant;
import com.railway.msg.model.MsgRecive;
import com.railway.msg.service.IMsgReciveManager;
import com.railway.msg.vo.query.MsgReciveQuery;
import com.railway.msg.vo.query.MsgSendQuery;

/**
 * @author jiangcheng
 * @version 1.0
 * @since 1.0
 */

public class MsgReciveAction extends BaseStruts2Action implements Preparable,
		ModelDriven {
	// 默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null;
	// forward paths
	protected static final String QUERY_JSP = "/admin/msg/msgRecive/query.jsp";
	protected static final String LIST_JSP = "/admin/msg/msgRecive/list.jsp";
	protected static final String CREATE_JSP = "/admin/msg/msgRecive/create.jsp";
	protected static final String EDIT_JSP = "/admin/msg/msgRecive/edit.jsp";
	protected static final String SHOW_JSP = "/admin/msg/msgRecive/show.jsp";
	// redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/msg/MsgRecive/list.do";
	private IMsgReciveManager msgReciveManager;
	private MsgRecive msgRecive;
	java.lang.String id = null;
	private String[] items;
	private String param;

	public void setParam(String param) {
		this.param = param;
	}

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			msgRecive = new MsgRecive();
		} else {
			msgRecive = (MsgRecive) msgReciveManager.getById(id);
		}
	}

	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setMsgReciveManager(IMsgReciveManager manager) {
		this.msgReciveManager = manager;
	}

	public Object getModel() {
		return msgRecive;
	}

	public String getMsgCount() {
  		String current_user_id = (String) this.getRequest().getSession()
				.getAttribute("userId");
 
		Map map = new HashMap();

		if (current_user_id != null && !current_user_id.equals("")) {
			try {
				long temp = msgReciveManager.getMsgCountByUserID(current_user_id);
	 
				map.put(IConstant.MSG_COUNT, temp);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return writeAjaxResponse(Json.toJson(map));
	}

	public void setId(java.lang.String val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	/** 执行搜索 */
	public String list() {
		return LIST_JSP;
	}

	public String updateMsgStatus() {

		if (items != null)
			for (String ids : items) {
				String[] idArray = ids.split(",");
				for (String id : idArray) {

					MsgRecive temp = msgReciveManager.getById(id);
					temp.setId(id);
					temp.setStatus(IConstant.MSG_STATUS_READED);//已读
					temp.setReadTime(new java.sql.Timestamp(System.currentTimeMillis()));
					/*SimpleDateFormat sdf = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					temp.setReadTimeString(sdf.format(new Date()));*/

					msgReciveManager.update(temp);
				}

			}

		return writeAjaxResponse("");
	}
	public String  getAllReciveMsgs(){
		String current_user_id = (String) this.getRequest().getSession()
		.getAttribute("userId");
		Map map = new HashMap();
		if (current_user_id != null && !current_user_id.equals("")) {

			MsgReciveQuery query = newQuery(MsgReciveQuery.class,
			DEFAULT_SORT_COLUMNS);
			query.setRecivor(current_user_id);
			query.setSortColumns("read_Time DESC");
	try {
		Page page = msgReciveManager.findPage(query);

		//更新后台相应消息的状态为 已读,另外更新阅读时间 
		List list = page.getResult();
		//将消息放入session,以便后面消息页显示给用户时,更新消息状态
		//
		map.put("total", page.getTotalCount());
		map.put("rows", page.getResult());
	} catch (Exception e) {
		e.printStackTrace();
	}
}
return writeAjaxResponse(Json.toJson(map));
		
	}
	public String getReciveMsgs() {

		String current_user_id = (String) this.getRequest().getSession()
				.getAttribute("userId");
		Map map = new HashMap();
		if (current_user_id != null && !current_user_id.equals("")) {

			MsgReciveQuery query = newQuery(MsgReciveQuery.class,
					DEFAULT_SORT_COLUMNS);
			query.setRecivor(current_user_id);
			query.setStatus(IConstant.MSG_STATUS_UNREAD);
			query.setSortColumns("read_Time DESC");
			try {
				Page page = msgReciveManager.findPage(query);

				//更新后台相应消息的状态为 已读,另外更新阅读时间 
				List list = page.getResult();
				//将消息放入session,以便后面消息页显示给用户时,更新消息状态
			
				map.put("total", page.getTotalCount());
				map.put("rows", page.getResult());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return writeAjaxResponse(Json.toJson(map));
	}
	public String showUnreadUsers(){
		return "/admin/msg/msgRecive/unreadlist.jsp";
	}
	/**
	 * 查看当前用户发出的未读其消息的用户列表
	 * @return
	 */
	public String listAjaxUnreadUsers() {
		String current_user_id = (String) this.getRequest().getSession().getAttribute(SysConstant.USER_ID);
		String msg_id = this.getRequest().getParameter(SysConstant.MSG_ID);
		MsgReciveQuery query = newQuery(MsgReciveQuery.class, DEFAULT_SORT_COLUMNS);
		query.setMsgId(msg_id);
		query.setStatus(IConstant.MSG_STATUS_UNREAD);
		query.setCreator(current_user_id);
		query.setSortColumns("read_Time DESC");
		Page page = msgReciveManager.findPageUnreadUsers(query);
		
		Map map = new HashMap();
		map.put("total", page.getTotalCount());
		map.put("rows", page.getResult());
		return writeAjaxResponse(Json.toJson(map));
	}
	public String listAjax() {

		MsgReciveQuery query = newQuery(MsgReciveQuery.class,
				DEFAULT_SORT_COLUMNS);
		query.setSortColumns("read_Time DESC");
		Page page = msgReciveManager.findPage(query);
		
		Map map = new HashMap();
		map.put("total", page.getTotalCount());
		map.put("rows", page.getResult());
		return writeAjaxResponse(Json.toJson(map));
	}

	/** 查看某个消息 */
	public String show() {
        String msgId = this.getRequest().getParameter("msgID");
        msgId=msgId.replaceAll("'", "");
        MsgRecive temp = msgReciveManager.getById(msgId);
        
		msgRecive.setStatus(IConstant.MSG_STATUS_READED);//已读
		msgRecive.setReadTime(new java.sql.Timestamp(System.currentTimeMillis()));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
		//msgRecive.setReadTimeString(sdf.format(new Date()));
		msgRecive.setContent(temp.getContent());

		msgReciveManager.update(msgRecive);
		return writeAjaxResponse("");//SHOW_JSP;
	}

	/** 进入新增页面 */
	public String create() {
		return CREATE_JSP;
	}

	/** 保存新增对象 */
	public String save() {
		msgReciveManager.save(msgRecive);
		return LIST_ACTION;
	}

	/** 进入更新页面 */
	public String edit() {
		return EDIT_JSP;
	}

	/** 保存更新对象 */
	public String update() {
		msgReciveManager.update(this.msgRecive);
		return LIST_ACTION;
	}

	/** 删除对象 */
	public String delete() {
		if (items != null)
			for (String ids : items) {
				String[] idArray = ids.split(",");
				for (String id : idArray){
					msgReciveManager.removeById(id);
//					MsgRecive  t = msgReciveManager.getById(id);
//					t.setStatus(IConstant.MSG_STATUS_DELETED);//对接收消息标记删除
//					msgReciveManager.update(t);
				}
			}
		return LIST_ACTION;
	}

}
